package net.xju.dao;

import net.xju.common.utils.JdbcUtilsV1;
import net.xju.entity.User;

import java.util.List;

public class UserDao {
    public User login(String username, String password) {
        String sql = "SELECT id, username, password, age, gender FROM Users WHERE username = ? AND password = ?";
        return JdbcUtilsV1.executeQueryOne(JdbcUtilsV1.getConnection(), User.class, sql, username, password);
    }

    public List<User> list() {
        String sql = "SELECT id, username, age, gender FROM Users";
        return JdbcUtilsV1.executeQuery(JdbcUtilsV1.getConnection(), User.class, sql);
    }

    public boolean insert(User user) {
        String sql = "INSERT INTO Users (username, password, age, gender) VALUES (?,?,?,?)";
        int i = JdbcUtilsV1.executeUpdate(sql, user.getUsername(), user.getPassword(), user.getAge(), user.getGender());
        return i > 0;
    }

    public boolean update(User user) {
        String sql = "UPDATE Users SET username = ?, password = ?, age = ?, gender = ? WHERE id = ?";
        int i = JdbcUtilsV1.executeUpdate(sql, user.getUsername(), user.getPassword(), user.getAge(), user.getGender(), user.getId());
        return i > 0;
    }

    public boolean delete(int id) {
        String sql = "DELETE FROM Users WHERE id = ?";
        int i = JdbcUtilsV1.executeUpdate(sql, id);
        return i > 0;
    }
}